#!/usr/bin/perl
package commify;

require Exporter;


our @ISA = qw(Exporter);
our $VERSION = 1.00;
our @EXPORT = qw(comma);

use strict;
use warnings;
use Carp;


#
# 	Adds commas or a user specified string
# 		every three digits
# 
# 
sub comma
{
	
	my ($number,  $delimiter) = @_;
    $delimiter ||= ',';
	#
	# 	ignore fraction after decimal point
	my($int, $frac) = split /\./, $number, 2;
	my @a = ();
	
	while($int =~ /\d{4}/)
	{
		# remove three digits at a time from back
		$int =~ s/(\d{3})$//;
		unshift @a, $1;
	}

	#add last remaining digits
	unshift @a, $int;

	#join back up
	$int = join $delimiter, @a;

	return defined $frac ? "$int\.$frac" : $int;
}


#print comma(1234567.1), "\n";
#print comma(1234567), "\n";
#print comma(-123456.232E34), "\n";
#print comma("123.00"), "\n";
1;
